package com.cabrida.livestar.main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.model.FansCallStar;
import com.cabrida.livestar.star.StarLeaveMsgActivity;
import com.cabrida.livestar.util.MyUtils;

/**
 * 呼唤明星adapter
 * 
 * @author why
 * 
 */
public class FansCallStarLvAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context mContext;
	private ArrayList<FansCallStar> data_list = new ArrayList<FansCallStar>();

	public FansCallStarLvAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
	}

	public void setData(ArrayList<FansCallStar> data) {
		data_list = data;
	}

	@Override
	public int getCount() {
		return data_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(
					R.layout.adapter_callstar_lv_item, null);
			
			holder.tv_lv = (TextView) convertView.findViewById(R.id.tv_lv);
			holder.iv_header = (ImageView) convertView.findViewById(R.id.iv_header);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_like_num = (TextView) convertView.findViewById(R.id.tv_like_num);
			holder.tv_msg_num = (TextView) convertView.findViewById(R.id.tv_msg_num);
			holder.tv_add_star = (TextView) convertView.findViewById(R.id.tv_add_star);
			holder.tv_add_num = (TextView) convertView.findViewById(R.id.tv_add_num);
			holder.ll_msg_num = (LinearLayout) convertView.findViewById(R.id.ll_msg_num);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		
		MyUtils.loadImg(data_list.get(position).getS_image(), holder.iv_header);
		if (position==0) {
			holder.tv_lv.setTextColor(mContext.getResources().getColor(R.color.red));
		}else if (position==1||position==2) {
			holder.tv_lv.setTextColor(mContext.getResources().getColor(R.color.red_coo));
		}else {
			holder.tv_lv.setTextColor(mContext.getResources().getColor(R.color.text_999));
		}
		holder.tv_lv.setText(String.valueOf(position+1));
		holder.tv_name.setText(data_list.get(position).getS_name());
		holder.tv_like_num.setText(data_list.get(position).getS_number());
		holder.tv_msg_num.setText(data_list.get(position).getS_comment());
		holder.tv_add_num.setText(data_list.get(position).getNum());
		
		holder.tv_add_star.setTag(position);
		holder.tv_add_star.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int pos = (Integer) v.getTag();
				mistener.onCallStar(data_list.get(pos).getSid());
			}
		});
		
		holder.ll_msg_num.setTag(position);
		holder.ll_msg_num.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View  v) {
				// TODO Auto-generated method stub
				int pos = (Integer) v.getTag();
				Bundle b = new Bundle();
				b.putString("suid", data_list.get(pos).getSid());
				b.putInt("type", Global.MESSAGE_TYPE_1);
				Intent intent = new Intent(mContext,StarLeaveMsgActivity.class);
				intent.putExtras(b);
				mContext.startActivity(intent);
			}
		});
		
		return convertView;
	}

	class ViewHolder {
		private TextView tv_lv;
		private ImageView iv_header;
		private TextView tv_name;
		private TextView tv_like_num;
		private TextView tv_msg_num;
		private TextView tv_add_star;
		private TextView tv_add_num;
		private LinearLayout ll_msg_num;
	}

	private OnCallStarListener mistener;

	public interface OnCallStarListener {
		public void onCallStar(String s_id);
	}

	public void setOnCallStarListener(OnCallStarListener listener) {
		mistener = listener;
	}
	
}
